Skip to content

Upgrade cudarc to 0.16.3#3

Closed
zackangelo wants to merge 1 commit intoEricLBuehler:masterfrom
zackangelo:cudarc_0_16
Closed

Upgrade cudarc to 0.16.3#3
zackangelo wants to merge 1 commit intoEricLBuehler:masterfrom
zackangelo:cudarc_0_16

Conversation

@zackangelo
Copy link

@zackangelo zackangelo commented Jun 11, 2025

Long term, probably want to add the DeviceRepr impls to the cudarc crate just like half does?

Upping cudarc to 0.16 so it's compatible with candle.

Summary by CodeRabbit

  • Chores

    • Updated dependency versions for improved compatibility and maintenance.
    • Reformatted feature lists in configuration for better readability.
  • Refactor

    • Simplified internal trait implementations for certain types, relying on default behavior. No changes to user-facing functionality.

@coderabbitai
Copy link

coderabbitai bot commented Jun 11, 2025

Walkthrough

Dependency versions for rand_distr and cudarc were updated in Cargo.toml, and several feature lists were reformatted for readability without changing their contents. In src/lib.rs, custom trait method overrides for device representation were removed, leaving only empty unsafe impl blocks for the relevant traits.

Changes

File(s) Change Summary
Cargo.toml Updated rand_distr and cudarc versions; reformatted features arrays for rand, cudarc, mistralrs_cudarc_fork, and all for readability. No feature or flag changes.
src/lib.rs Removed custom as_kernel_param implementations for DeviceRepr traits on F8E4M3 and F8E5M2; now using empty unsafe impls.

Poem

In the garden of code, new versions bloom bright,
Dependencies pruned, arrays set just right.
Traits now stand silent, no custom to show,
As rabbits hop forward, the kernels still flow.
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

addr_of!(self.0) as *const u8 as *mut _
}
}
unsafe impl cudarc::driver::DeviceRepr for F8E4M3 {}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure why cudarc removed as_kernel_param, could not find any documentation about that change. It was part of a much larger code reorganization.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 33c1797 and 03c1f5f.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (2)
  • Cargo.toml (1 hunks)
  • src/lib.rs (1 hunks)
🔇 Additional comments (1)
Cargo.toml (1)

31-41:

Details

❓ Verification inconclusive

Verify that every listed feature name still exists in cudarc 0.16.x

cudarc bumped three minor versions (0 .13 → 0 .16). In that time several cargo-feature flags were renamed (f16f16-types, cuda-version-from-build-systemcuda-version-from-build-env, etc.).
If any of the names shown here disappeared, cargo will fail at feature-resolution time and the whole build breaks before compilation even starts.

Before merging, run a clean cargo check --all-features (or CI) to confirm the feature set is still valid, and drop / rename flags that no longer exist.
If you need a quick way to inspect the new feature list:


🏁 Script executed:

cargo metadata --format-version=1 --no-deps \
| jq -r '.packages[] | select(.name=="cudarc") | .features | keys[]' | sort

Length of output: 368


Manual verification required: confirm cudarc feature flags

The automated cargo metadata check failed due to no default Rust toolchain. Before merging, please:

  • Set a default Rust toolchain (for example, rustup default stable).
  • Run:
    cargo metadata --format-version=1 --no-deps \
      | jq -r '.packages[] | select(.name=="cudarc") | .features | keys[]' | sort
    
  • Verify that each feature listed in your Cargo.toml block (lines 31–41) still exists in cudarc 0.16.x.
  • Remove or rename any flags that have been renamed or removed (e.g. f16f16-types, cuda-version-from-build-systemcuda-version-from-build-env).

If you prefer not to configure your local toolchain, you can confirm via your CI by running cargo check --all-features.

Comment on lines +1568 to +1575
unsafe impl cudarc::driver::DeviceRepr for F8E4M3 {}

#[cfg(feature = "cuda")]
unsafe impl cudarc::driver::ValidAsZeroBits for F8E4M3 {}

#[cfg(feature = "cuda")]
unsafe impl cudarc::driver::safe::DeviceRepr for F8E5M2 {
fn as_kernel_param(&self) -> *mut std::ffi::c_void {
use std::ptr::addr_of;
addr_of!(self.0) as *const u8 as *mut _
}
}
unsafe impl cudarc::driver::safe::DeviceRepr for F8E5M2 {}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Inconsistent use of DeviceRepr vs safe::DeviceRepr between the two types

F8E4M3 → implements driver::DeviceRepr
F8E5M2 → implements driver::safe::DeviceRepr

Unless there is a documented reason that one type must be “unsafe-param” and the other the “safe” variant, this asymmetry is surprising and makes the maintenance story harder (especially after the cudarc bump).

  1. If both types satisfy the invariants of the safe trait, prefer that for both:
 #[cfg(feature = "cuda")]
 unsafe impl cudarc::driver::DeviceRepr for F8E4M3 {}
+#[cfg(feature = "cuda")]
+unsafe impl cudarc::driver::safe::DeviceRepr for F8E4M3 {}
  1. If the old, now-removed as_kernel_param implementation is still required for the unsafe trait in 0 .16, the build will fail with a trait-method-not-implemented error. Make sure cargo check --features=cuda passes.

Please double-check the cudarc 0.16 trait docs and align the two impls accordingly.


🏁 Script executed:

#!/bin/bash
# Inspect implementations of DeviceRepr and safe::DeviceRepr in src/lib.rs around F8E4M3 and F8E5M2
rg -n "F8E4M3" src/lib.rs
rg -n "F8E5M2" src/lib.rs
sed -n '1550,1600p' src/lib.rs
rg -n "DeviceRepr" src/lib.rs

Length of output: 12573


🌐 Web query:

In cudarc 0.16, what are the definitions and requirements of cudarc::driver::DeviceRepr and cudarc::driver::safe::DeviceRepr traits?

💡 Result:

Definitions and Requirements of DeviceRepr in cudarc 0.16

cudarc::driver::DeviceRepr

The DeviceRepr trait in cudarc::driver is an unsafe Rust trait that describes types whose memory representation is suitable for copying between the host (CPU) and the device (GPU) using CUDA's driver API. In other words, it is a marker for types that can be safely and correctly transferred between Rust and CUDA device memory without introducing undefined behavior.

Requirements:

  • The trait is generally implemented for types that are #[repr(C)] or #[repr(transparent)] and contain only CUDA-compatible primitives (e.g., integers, floats).
  • Implementors must ensure that their type's memory layout matches what CUDA expects so that data can be faithfully reinterpreted on the device.
  • Types using custom representations or containing references, pointers, or Rust-managed resources must not implement this trait.
  • The trait is inherently unsafe, as incorrect implementations can lead to undefined behavior.

Typical signature:

pub unsafe trait DeviceRepr: Copy + Sized {}

cudarc::driver::safe::DeviceRepr

The DeviceRepr trait under the cudarc::driver::safe module refers to the same concept as above but is exposed through cudarc's "safe" API surface. This module wraps the lower-level, potentially unsafe CUDA driver interactions in a safer Rust interface wherever possible.

Requirements and Guarantees:

  • The trait in safe is typically a re-export or a wrapper of the root cudarc::driver::DeviceRepr, enforcing the same requirements and constraints: the type must have a valid, device-compatible binary representation.
  • In the "safe" context, this trait is used to generically constrain types that can be transferred or viewed in device memory using abstractions like CudaSlice<T>, CudaView<T>, and CudaViewMut<T>, which provide more ergonomic and memory-safe device buffer management[3].
  • The use of this trait helps enforce that only "POD" (plain-old-data) types can be moved to CUDA device memory, reducing user error in memory transfers.

Summary Table

Trait Location Type of Trait Key Requirements/Guarantees
cudarc::driver::DeviceRepr Unsafe Only for types compatible with CUDA device memory, must be Copy, Sized, and have a C-compatible layout; unsafe to implement incorrectly.
cudarc::driver::safe::DeviceRepr Safe API Same requirements as above; used in higher-level abstractions for safer device memory access.

Practical Usage

These traits are primarily used to constrain the types that can be passed to or received from device buffers. They protect against common errors, such as attempting to transfer complex Rust types (structs with references, heap allocations, or non-C-compatible layouts) directly to the GPU, which would result in undefined or erroneous behavior.

"Both of these traits can generically accept memory allocated by the driver in the form of: driver::CudaSlice<T>, driver::CudaView<T>, and driver::CudaViewMut<T>..."[3]

Conclusion

  • DeviceRepr is a marker trait for types that are plain-old-data and have a memory layout directly compatible with CUDA device memory.
  • Only safe, simple types (like primitives and #[repr(C)] structs containing them) should implement these traits.
  • The trait is declared as unsafe in the core API and is used as a bound in cudarc's safe, ergonomic abstractions for device memory buffers[3].

If you require the exact trait definition, it can be reviewed in the cudarc source code on docs.rs[1] or the project's GitHub repository, but the above summarizes its definition and requirements accurately.

Citations:


Align DeviceRepr impls for F8E4M3 under both CUDA features

In cudarc 0.16 the safe::DeviceRepr trait has identical requirements to the root DeviceRepr but is the preferred, higher-level API. Currently:

  • Under feature = "cuda",
    • F8E4M3 implements cudarc::driver::DeviceRepr
    • F8E5M2 implements cudarc::driver::safe::DeviceRepr

  • Under feature = "mistralrs_cudarc_fork",
    • F8E4M3 implements mistralrs_cudarc_fork::driver::DeviceRepr
    • F8E5M2 implements mistralrs_cudarc_fork::driver::safe::DeviceRepr

Please make them symmetric by adding the missing safe impls for F8E4M3:

• In src/lib.rs under #[cfg(feature = "cuda")], after

unsafe impl cudarc::driver::DeviceRepr for F8E4M3 {}

add

#[cfg(feature = "cuda")]
+unsafe impl cudarc::driver::safe::DeviceRepr for F8E4M3 {}

• In the same file under #[cfg(feature = "mistralrs_cudarc_fork")], after

unsafe impl mistralrs_cudarc_fork::driver::DeviceRepr for F8E4M3 {
    fn as_kernel_param(&self) -> *mut std::ffi::c_void {}
}

add

#[cfg(feature = "mistralrs_cudarc_fork")]
+unsafe impl mistralrs_cudarc_fork::driver::safe::DeviceRepr for F8E4M3 {
+    fn as_kernel_param(&self) -> *mut std::ffi::c_void {
+        use std::ptr::addr_of;
+        addr_of!(self.0) as *const u8 as *mut _
+    }
+}

Then verify:

cargo check --features="cuda mistralrs_cudarc_fork"

to ensure you haven’t introduced any missing trait errors.

🤖 Prompt for AI Agents
In src/lib.rs around lines 1568 to 1575, the implementations of DeviceRepr
traits for F8E4M3 and F8E5M2 are inconsistent: F8E4M3 implements
cudarc::driver::DeviceRepr while F8E5M2 implements
cudarc::driver::safe::DeviceRepr under the "cuda" feature. To fix this, add an
unsafe impl of cudarc::driver::safe::DeviceRepr for F8E4M3 under the
#[cfg(feature = "cuda")] block after the existing DeviceRepr impl. Similarly,
under the #[cfg(feature = "mistralrs_cudarc_fork")] feature, add the
safe::DeviceRepr impl for F8E4M3 after its DeviceRepr impl. Finally, run cargo
check with both features enabled to confirm no trait errors remain.

@polarathene
Copy link

Upping cudarc to 0.16 so it's compatible with candle.

Cross-referencing, for mistral.rs this version bump should probably be aligned with the Candle fork it's dependent upon: EricLBuehler/candle#83 (comment)

@zackangelo
Copy link
Author

superceded by #5

@zackangelo zackangelo closed this Aug 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants